// pages/index/zone/filterpage/filterpage.js
let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scrollHeight: '', //筛选区域scrollview高度,
        data: [],    //存放获取到的上一页面的筛选总数据,然后进行筛选
        prevPage: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            scrollHeight: app.globalData.windowHeight * 2 - 100
        })

        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];  // 获取上一页实例
        this.setData({
            data: prevPage.data.filterData,   //上一页的筛选总数据
            prevPage: prevPage
        })
        console.log(this.data.data)
    },

    //选择
    choosethis(e) {
        let { id, type } = e.currentTarget.dataset;
        console.log(id, type)
        let data = this.data.data
        let arr = data.find(item => item.type == type)
        console.log(arr.isMultiple)
        //多选项
        if (arr.isMultiple) {
          //点击到 全部 
          if (id==''){
            arr.attr.map(x => x.selected = false)
            arr.attr.find(x => x.id == '').selected = true
          //点击到其他
          }else{
            arr.attr.find(x => x.id == '').selected = false
            arr.attr.find(x => x.id == id).selected = !arr.attr.find(x => x.id == id).selected
          }
        //单选项
        } else {
          arr.attr.map(x => x.selected = false)
          arr.attr.find(x => x.id == id).selected = !arr.attr.find(x => x.id == id).selected
        }
        console.log(data)
        this.setData({
            data: data
        })
    },

    //重置
    reset() {
        let data = this.data.data;
        //清空selected
        data.forEach(item => {
            item.attr.forEach(it => {
                it.selected = (it.id == '') ? true : false
            })
        })
        this.setData({
            data: data
        })
    },

    //完成
    done() {
        //把筛选的数据发送到上一页面，并且调用上一页的changeFilterData方法
        this.data.prevPage.changeFilterData(this.data.data);
        wx.navigateBack();
    }
})